package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.commons.exception.TanHuaException;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.PeachBlossom;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.mongo.Voices;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.PeachBlossomVo;
import com.tanhua.domain.vo.VideoVo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.PeachblossomApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.List;

/**
 * @ClassName PeachblossomService
 * @Description TODO
 * @Author zxs
 * @Date 2021/4/20 10:39
 * Version 1.0
 */

/**
 * 桃花传音-业务层
 */
@Service
public class PeachblossomService {

    @Autowired
    private FastFileStorageClient fastClient;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    @Reference
    private PeachblossomApi voicesApi;

    @Reference
    private UserInfoApi userInfoApi;

    //桃花传音-发送语音
    public void voices(MultipartFile soundFile) {
        //判断一下接受到的语音，如果为空则不处理
        if (soundFile != null && soundFile.getSize() > 0) {

            Long userId = UserHolder.getUserId();
            //查询当前本人信息
            UserInfo pbUserInfo = userInfoApi.findByUserId(userId);
            try {

                String filename = soundFile.getOriginalFilename();
                String suffix = filename.substring(filename.lastIndexOf(".") + 1);
                StorePath storePath = fastClient.uploadFile(soundFile.getInputStream(), soundFile.getSize(), suffix,null);
                String soundUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
                PeachBlossom peachBlossom = new PeachBlossom();
                peachBlossom.setUserId(userId);
                peachBlossom.setSoundUrl(soundUrl);
                peachBlossom.setCreated(System.currentTimeMillis());
                peachBlossom.setGender(pbUserInfo.getGender());
                voicesApi.save(peachBlossom);
            } catch (IOException e) {
                e.printStackTrace();
                throw new TanHuaException("上传语音失败!");
            }
        }
    }

    //桃花传音-接收语音
    public PeachBlossomVo findVoice() {
        Long userId = UserHolder.getUserId();
        String dateTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String key = userId + "_" +dateTime + "_PEACHBLOSSON";
        String value = redisTemplate.opsForValue().get(key);
        if (value == null) {
            redisTemplate.opsForValue().set(key,"5", Duration.ofDays(1));
        }else if (value.equals("0")){
            return null;
        }else {
            //decrement表示递减
            redisTemplate.opsForValue().decrement(key);
        }

        //当前登录用户
        UserInfo loginUserInfo = userInfoApi.findByUserId(userId);
        //查询到的语音用户
        PeachBlossom peachBlossom = voicesApi.findVoice(userId,loginUserInfo.getGender());


        UserInfo pbUserInfo = userInfoApi.findByUserId(peachBlossom.getUserId());
        PeachBlossomVo vo = new PeachBlossomVo();
        vo.setId(peachBlossom.getUserId().intValue());
        vo.setAvatar(pbUserInfo.getAvatar());
        vo.setNickname(pbUserInfo.getNickname());
        vo.setAge(pbUserInfo.getAge());
        vo.setGender(pbUserInfo.getGender());
        vo.setSoundUrl(peachBlossom.getSoundUrl());
        vo.setRemainingTimes(Integer.parseInt(redisTemplate.opsForValue().get(key)));
        return vo;
    }
}
